package testLambda;

import java.util.List;

/**
 * lambda forach  与 自带的  foreach 
 * 
 * lambda foreach 比 自带的foreach 快 30%
 * 
 * @author jinmiao
 * 2016年7月10日
 *
 */
public class Lambda_Foreach_test {
	public static void main(String[] args) {
		List<LambdaBean> list = LambdaBean.create(100);
		// 性能测试
		for (int i = 0; i < 20000000; i++) {
			lambda(list);
			normal(list);
		}
		long start = System.currentTimeMillis();
		for (int i = 0; i < 20000000; i++) {
			lambda(list);
		}
		System.out.println(System.currentTimeMillis() - start);
		start = System.currentTimeMillis();
		for (int i = 0; i < 20000000; i++) {
			normal(list);
		}
		System.out.println(System.currentTimeMillis() - start);

	}

	private static void lambda(List<LambdaBean> list) {
		// System.out.println(list.hashCode());
		list.forEach(id -> {
			id.getI();
		});
		// System.out.println(list.hashCode());
		// list.forEach(id->{System.out.println(id);});
	}

	private static void normal(List<LambdaBean> list) {
		for (LambdaBean id : list) {
			id.getI();
		}
//		int size = list.size();
//		List<LambdaBean> l = list;
//		for (int i =0;i<size;i++) {
//			l.get(i).getI();
//		}
		
		
		// res.forEach(id->{System.out.println(id);});
	}
}
